<?php
namespace app\admin\controller;
use controller\BasicAdmin;
use service\ExtendService;
use service\LogService;
use service\DataService;
use think\Db;
/**
 * 后台参数配置控制器
 * Class Config
 * @package app\admin\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/02/15 18:05
 */
class Config extends BasicAdmin
{

    /**
     * 当前默认数据模型
     * @var string
     */
    public $table = 'SystemConfig';

    /**
     * 当前页面标题
     * @var string
     */
    public $title = '网站参数配置';

    /**
     * 显示系统常规配置
     */
    public function index()
    {
        if (!$this->request->isPost()) {
            return view('', ['title' => $this->title]);
        }
        foreach ($this->request->post() as $key => $vo) {
            sysconf($key, $vo);
        }
        LogService::write('系统管理', '系统参数配置成功');
        $this->success('系统参数配置成功！', '');
    }

    /**
     * 文件存储配置
     */
    public function file()
    {
        $alert = [
            'type'    => 'danger',
            'title'   => '操作安全警告（默认使用本地服务存储）',
            'content' => '请根据实际情况配置存储引擎，合理做好站点下载分流。建议尽量使用云存储服务，同时保证文件访问协议与网站访问协议一致！'
        ];
        $this->title = '文件存储配置';
        $this->assign('alert', $alert);
        return $this->index();
    }

    /**
     * @Author lignyun
     * @Desc    网站系统配置
     */
    public function webConfig(){
        $this->title = '参数配置';
        $table = "DicConfig";
        $get = $this->request->get();
        $map['is_deleted'] = 0;
        $db = Db::name($table)->where($map);
        foreach (['detail'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        return parent::_list($db);
    }

    /**
     * @Author lignyun
     * @Desc    添加 -- 编辑配置蚕食
     */
    public function add_webConfig(){
        if(request()->isPost()){
            $post = input('post.');
            if(empty($post['id'])){
                $post['addtime'] = time();
                $res = Db::name('dic_config')->insert($post);
                $res>0?$this->success('保存成功!',''):$this->error('保存失败','');
            }else{
                $res = Db::name('dic_config')->update($post);
                $res!==false?$this->success('保存成功！',''):$this->error('保存失败','');
            }
        }else{
            $id = input('id');
            $info = Db::name('dic_config')->where('id',$id)->find();
            $this->assign('info',$info);
            return $this->fetch('add_config');
        }
    }
    /**
     * @Author lignyun
     * @Desc    删除 -- 配置信息
     */
    public function del_config(){
        $table = 'DicConfig';
        if (DataService::update($table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }



    /**
     * @Author  lingyun
     * @Desc    会员充值规则设置
     */
    public function recharge_config(){
        $this->title = '充值活动';
        $table = "RechargeRule";
        $get = $this->request->get();
        $map['is_deleted'] = 0;
        $db = Db::name($table)->where($map);
        foreach (['detail'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        return parent::_list($db);
    }

    /**
     * @Author  lingyun
     * @Desc    添加充值规则
     */
    public function add_recharge_rule(){
        if(request()->isPost()){
            $post = input('post.');
            if(empty($post['id'])){
                $post['addtime'] = time();
                $res = Db::name('recharge_rule')->insert($post);
                $res>0?$this->success('保存成功!',''):$this->error('保存失败','');
            }else{
                $res = Db::name('recharge_rule')->update($post);
                $res!==false?$this->success('保存成功！',''):$this->error('保存失败','');
            }
        }else{
            $id = input('id');
            $info = Db::name('recharge_rule')->where('id',$id)->find();
            !empty($info['amount'])?$info['amount'] = (int)$info['amount']:'';             //充值金额，转整形
            !empty($info['give_away'])?$info['give_away']=(int)$info['give_away']:'';       //赠送金额，转整形
            $this->assign('info',$info);
            return $this->fetch('add_recharge_rule');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    删除会员充值规则
     */
    public function del_recharge_rule(){
        $id = input('id');
        $res = db('recharge_rule')->where('id','in',$id)->delete();
        $res!==false?$this->success('删除成功',''):$this->error('删除失败','');
    }

}
